//
//  VKMeJFSMController.m
//  V-Learn
//
//  Created by liubo on 2019/1/9.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKReNextLevelController.h"
#import "VKReNextLevelCell.h"
@interface VKReNextLevelController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *totalLabel;


@end

@implementation VKReNextLevelController
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT + 1, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 1 - 50) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的下级";
    self.view.backgroundColor = [UIColor whiteColor];

    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupUI];
    [self tableView];
    [self requestNextLeavelData];
    
}
- (void)requestNextLeavelData{
    
    [WYHXNetWorkManager get:WKQueryUserSubordinate_WKURL params:@{@"token":[UserModel getUserModel].token} completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        NSString *code = responseObj[@"code"];
        NSString *msg = responseObj[@"msg"];
        
        if ([code integerValue]==200) {
            NSString *allMoney = responseObj[@"data"][@"allMoney"];
            NSArray *arr = [VKReNextLevelModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"userJuniorVOList"]];
            [self.dataArray addObjectsFromArray:arr];
            NSString *total = [NSString stringWithFormat:@" ¥%@",allMoney];
            [self downCountAttributedleftString:@"合计:"  withRightString:total withLabel:self.totalLabel];
        }else if ([code integerValue]==202){
            [MBProgressHUD showSuccess:msg toView:self.view];
            return ;
        }
        [self.tableView reloadData];
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKReNextLevelCell *cell = [VKReNextLevelCell cellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setupUI{
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [self.view addSubview:titleLabel];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.text = @"我的下级";
    titleLabel.font = [UIFont systemFontOfSize:19];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(LBNAVBAR_HEIGHT - 40);
    }];
    
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.backgroundColor = [UIColor clearColor];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@12);
        make.width.height.equalTo(@40);
        make.centerY.equalTo(titleLabel);
    }];
    
    UIImageView *imageBackV = [[UIImageView alloc]init];
    [btn addSubview:imageBackV];
    imageBackV.image = [UIImage imageNamed:@"back"];
    imageBackV.contentMode = UIViewContentModeScaleAspectFill;
    imageBackV.userInteractionEnabled = NO;
    [imageBackV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.centerX.equalTo(btn);
    }];
    [btn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *lineLabel = [[UILabel alloc]init];
    [self.view addSubview:lineLabel];
    lineLabel.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    [lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.equalTo(@1);
        make.top.mas_equalTo(LBNAVBAR_HEIGHT);
    }];
    
    UIView *downView = [[UIView alloc]init];
    [self.view addSubview:downView];
    downView.backgroundColor = [UIColor whiteColor];
    [downView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(@0);
        make.height.equalTo(@50);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [downView addSubview:button];
    button.backgroundColor = [UIColor cz_colorWithRed:235 green:81 blue:32];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.right.mas_equalTo(downView);
        make.width.equalTo(@120);
    }];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];

    UILabel *totalLabel = [[UILabel alloc] init];
    self.totalLabel = totalLabel;
    [downView addSubview:totalLabel];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.textAlignment = NSTextAlignmentCenter;
    [self downCountAttributedleftString:@"合计:"  withRightString:@" ¥0" withLabel:totalLabel];
    [totalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(button);
    }];
}
- (void)downCountAttributedleftString:(NSString *)leftString withRightString:(NSString *)rightString withLabel:(UILabel *)contentLabel {
    
    // 左边的文字颜色
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:leftString attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18.0]}];

    // 右边的文字颜色
    NSMutableAttributedString *rightS = [[NSMutableAttributedString alloc]initWithString:rightString attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
    [string insertAttributedString:rightS atIndex:string.length];
    contentLabel.attributedText = string;
}
- (void)buttonClick{
    
}

- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
